//
//  NSFileManager+Tools.m
//  CodeFactory
//
//  Created by daibaisheng on 2019/4/29.
//  Copyright © 2019 daibaisheng. All rights reserved.
//

#import "NSFileManager+Tools.h"

@implementation NSFileManager (Tools)

///判断路径文件是否存在
+(BOOL)isExistAtPath:(NSString *)path{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    return isExist;
}

//给NSFileManager添加分类,实现删除文件,创建文件,读取文件.
///删除指定路径下的文件
+(void)removeDirectory:(NSString *)path{
    if ([self isExistAtPath:path]){
        [[NSFileManager defaultManager]removeItemAtPath:path error:nil];
    }else{
        NSLog(@".sketchImage不存在,不用删除");
    }
}

///指定一个路径 创建一个新的文件夹
+(NSString *)createDirectory:(NSString *)path{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([self isExistAtPath:path]){
        NSLog(@"not create");
        return path;
    }else{
        BOOL isSuccess = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        if (isSuccess) {
            NSLog(@"success");
            return path;
        } else {
            NSLog(@"fail");
            return nil;
        }
    }
    return nil;
}

///获取路径下所有文件
+(NSArray*)fetchDirectory:(NSString*)path {
    NSMutableArray* array = [NSMutableArray array];
    if ([self isExistAtPath:path]){
        NSFileManager* fileMgr = [NSFileManager defaultManager];
        NSArray* tempArray = [fileMgr contentsOfDirectoryAtPath:path error:nil];
        for (NSString* fileName in tempArray) {
            BOOL flag = YES;
            NSString* fullPath = [path stringByAppendingPathComponent:fileName];
            if ([fileMgr fileExistsAtPath:fullPath isDirectory:&flag]) {
                if (!flag) {
                    [array addObject:fullPath];
                }
            }
        }
        return array;
    }
    
    NSLog(@"没有找到路径,无法读取");
    
    return array;
}


+(NSString *)getDesktopFilePath:(NSString *)fileName;
{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
    NSString* thepath = [paths lastObject];
    thepath = [thepath stringByAppendingPathComponent:fileName];
    return thepath;
}


+ (NSDictionary *)accessModelPlist:(NSString *)plistName type:(NSString *)fileType{
    NSString   *path = [[NSBundle mainBundle] pathForResource:plistName ofType:fileType];
    NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSArray *arrKeys = [data allKeys];
    return data;
}



@end
